#include <iostream>
#include <vector>

using namespace std;

class Person {
public:
    Person(const string &name):
        m_name(name), m_wife(nullptr) {
        std::cout << "constructor is called - " << m_name << std::endl;
    }
    ~Person() {
        std::cout << "deconstructor is called - " << m_name << std::endl;
    }

    /// 和系统默认生成的一样的功能
    Person(const Person &p) {
        m_name = p.m_name;
        m_wife = p.m_wife;
        std::cout << "copy constructor is called - " << m_name << std::endl;
    }

    Person& operator= (const Person &) {
        std::cout << "operate= is called" << std::endl;
    }


    Person *wife() const
    {
        return m_wife;
    }
    void setWife(Person *wife)
    {
        m_wife = wife;
    }

protected:
    string m_name;
    Person *m_wife;  // share_ptr<Person> m_wife;
};

class TestResourceFree {
public:
    static vector<Person> createPersonList()
    {
        Person wife("hua");
        Person wang("wang");
        wang.setWife(&wife);
        vector<Person> listPerson;
        listPerson.push_back(wang); // 调用copy构造函数

    // listPerson.push_back(Person("yang"));

        vector<Person> listPerson2 = listPerson;

        cout << "wang adress = " << addressof(wang) << " wife:" << wang.wife() << endl;

        cout << "listPerson[0] adress = " << addressof(listPerson[0]) << " wife:" << listPerson[0].wife() << endl;
        cout << "listPerson2[0] adress = " << addressof(listPerson2[0]) << " wife:" << listPerson2[0].wife() << endl;

        // listPerson 中 wife指向指针被释放掉了，这块怎么解决？ 可能用 shared_ptr 可以解决
        return listPerson;
    }

};

class TestVectorCopy {
public:
    class Cube {
    public:
        Cube(int age_) : age(age_) {}
        int age = 10;
        float height = 101.1;
    };

    static void insertArray() {
        Cube a[2] = {
            {1},
            {3}
        };

        vector<Cube> cubes;
        for(int i=0; i<2; i++) {
            cubes.push_back(a[i]);
        }

        for(int i=0; i< cubes.size(); i++) {
            printf("cube: %d\n", cubes[i].age);
        }
    }
};

int main(int argc, char *argv[])
{
    int op = 2;
    switch (op) {
    case 1:{
        vector<Person> listPerson3 = TestResourceFree::createPersonList();
        cout << "listPerson3[0] adress = " << addressof(listPerson3[0]) << " wife:" << listPerson3[0].wife() << endl;
        break;
    }
    case 2:
        TestVectorCopy::insertArray();
        break;
    }
    return 0;
}

